﻿using RdpClient.BLL;
using Common.Class;

namespace RdpClient.Forms
{
    public partial class FrmServerLogin : Form
    {
        int ErrorCount = 0;

        public FrmServerLogin()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
            this.MaximizeBox = false; //取消最大化按键
            this.MinimizeBox = false; //取消最小化按键
            this.ShowInTaskbar = false;//不在任务栏显示多个窗体框
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrWhiteSpace(txtUserID.Text))
            {
                MessageBox.Show("登录名不可为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                MessageBox.Show("登录密码不可为空");
                return;
            }

            if (!ServerBLL.UserLogin(txtUserID.Text.Trim(), txtPassword.Text.Trim()))
            {
                MessageBox.Show("用户名或密码错误");
                ErrorCount++;
                if (ErrorCount >= 3)
                {
                    this.DialogResult = DialogResult.Cancel;
                }
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                // 用户按下了回车键 
                e.Handled = true; // 阻止默认的换行行为 
                btnOK_Click(btnOK, null);
            }
        }
    }
}
